part of svgaplayer_flutter_player;

class SVGASimpleImage extends StatefulWidget {
  final String? resUrl;
  final String? assets;
  final BoxFit fit;
  final bool? repeat;
  final VoidCallback? onForward;
  final VoidCallback? onReverse;
  final VoidCallback? onCompleted;
  final VoidCallback? onDismissed;

  const SVGASimpleImage(
      {Key? key,
      this.resUrl,
      this.assets,
      this.fit = BoxFit.contain,
      this.repeat = true,
      this.onForward,
      this.onReverse,
      this.onCompleted,
      this.onDismissed})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _SVGASimpleImageState();
  }
}

class _SVGASimpleImageState extends State<SVGASimpleImage>
    with SingleTickerProviderStateMixin {
  SVGAAnimationController? animationController;

  @override
  void initState() {
    super.initState();
    animationController = SVGAAnimationController(vsync: this);

    animationController?.addStatusListener((status) {
      debugPrint('[svgaplayer] animationController status ------- $status');

      switch (status) {
        case AnimationStatus.forward:
          if (widget.onForward != null) {
            widget.onForward!();
          }
          break;
        case AnimationStatus.reverse:
          if (widget.onReverse != null) {
            widget.onReverse!();
          }
          break;
        case AnimationStatus.completed:
          if (widget.onCompleted != null) {
            widget.onCompleted!();
          }
          break;
        case AnimationStatus.dismissed:
          if (widget.onDismissed != null) {
            widget.onDismissed!();
          }
          break;
        default:
      }
    });

    _tryDecodeSvga();
  }

  @override
  void didUpdateWidget(covariant SVGASimpleImage oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.resUrl != widget.resUrl ||
        oldWidget.assets != widget.assets) {
      _tryDecodeSvga();
    }
  }

  @override
  Widget build(BuildContext context) {
    if (animationController == null) {
      return Container();
    }
    return SVGAImage(
      animationController!,
      fit: widget.fit,
    );
  }

  @override
  void dispose() {
    animationController?.dispose();
    animationController = null;
    super.dispose();
  }

  void _tryDecodeSvga() {
    Future<MovieEntity> decode;
    if (widget.resUrl != null) {
      decode = SVGAParser.shared.decodeFromURL(widget.resUrl!);
    } else if (widget.assets != null) {
      decode = SVGAParser.shared.decodeFromAssets(widget.assets!);
    } else {
      return;
    }
    decode.then((videoItem) {
      if (mounted && animationController != null) {
        animationController?.videoItem = videoItem;
        if (widget.repeat == true) {
          animationController?.repeat();
        } else {
          videoItem.autorelease = true;
          animationController?.forward();
        }
      } else {
        videoItem.dispose();
      }
    }).catchError((e, stack) {
      FlutterError.reportError(FlutterErrorDetails(
        exception: e,
        stack: stack,
        library: 'svga library',
        informationCollector: () => [
          if (widget.resUrl != null) StringProperty('resUrl', widget.resUrl),
          if (widget.assets != null)
            StringProperty('assetsName', widget.assets),
        ],
      ));
    });
  }
}
